package com.me.forum.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.Page;
import com.me.forum.request.PageRequest;
import com.me.forum.service.impl.PageService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/page")
public class AdminPageController {
    //by ly
    @Autowired
    private PageService pageService;

    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id,IModel model) {
        Page page = pageService.findById(id);
        model.put("success", page != null);
        model.put("page", page);
        return model;
    }

    @GetMapping
    public IModel getAllPages(PageRequest preq, IModel model, String page, String size) {
        IPage<Page> pages = pageService.getAll(preq,page,size);
        model.success(page!= null && pages.getSize() != 0);
        model.add(RespType.Page,pages);
        return model;
    }

    @PostMapping
    public IModel AddPage(@RequestBody PageRequest preq,IModel model) {
        model.success(pageService.addPage(preq));
        return model;
    }

    @PutMapping
    public IModel pageUpdate(@RequestBody PageRequest preq,IModel model) {
        model.success(pageService.updateByTitle(preq));
        return model;
    }
    //删除  delete
    @DeleteMapping
    private IModel pageDelete(String id,IModel model) {
        model.success(pageService.delete(id));
        return model;
    }
}
